/**
 * Created by 30947 on 2018/7/20.
 */
const baseUrl = "http://8.130.26.11:8002";
let isFirst = true;
function addMark(longitude, latitude) {
  if (window.baiduMap) {
    console.log(longitude, latitude);
    point = new BMap.Point(longitude, latitude);
    var marker = new BMap.Marker(point);
    window.baiduMap.addOverlay(marker);
  }
}
$(function () {
  nav();
  login();

  // setInterval(() => {
  //   insertData();
  // }, 1000);
});
//导航条点击添加样式
function nav() {
  //$(".nav>ul>li").click(function(){
  //    var href=$(this).find("a").attr("href");
  //   var t=href.indexOf("html");
  //    var ins;
  //    if(t!=-1){
  //      ins=$(this).index();
  //        $(".nav>ul>li").eq(ins).addClass("nav_active");
  //        window.location.href=href;
  //
  //
  //    }
  //    else{
  //        $(this).addClass("nav_active").siblings().removeClass("nav_active");
  //    }
  //
  //})
  $(".nav>ul>li").hover(
    function () {
      $(this).find(".li_ul").stop(true, true).slideDown("slow");
      stop();
    },
    function () {
      $(this).find(".li_ul").slideUp("slow");
    }
  );
}

//根据经纬度添加地图位置

function login() {
  var data = JSON.stringify({ userAccount: "admin", userPassword: "admin" });
  $.ajax({
    url: `${baseUrl}/smartLamp/user/login`,
    data: data,
    type: "POST",
    dataType: "json",
    contentType: "application/json",
    success: function (res) {
      console.log(res);
      localStorage.setItem("token", res.data.token);
    },
  }).then((res) => {
    addMark(117.05389754282878, 39.24395340578026);
    addMark(117.35389754282878, 39.26395340578026);
    addMark(117.45389754282878, 39.23395340578026);
    addMark(117.38389754282878, 39.25395340578026);
    addMark(117.324389754282878, 39.26395340578026);
    console.log(res);
    insertLamp1Data();
    insertLamp2Data();

    setInterval(() => {
      insertLamp1Data();
      insertLamp2Data();
    }, 5000);

    setInterval(() => {
      queryWarnInfo(1);
      queryWarnInfo(2);
    }, 1000);
  });
}
//时间转化
function dateTr(time) {
  var t = time.split(/[- : T .]/);
  var d = t[0] + "-" + t[1] + "-" + t[2];
  var h = t[3] + ":" + t[4] + ":" + t[5];
  return d + " " + h;
}
function insertLamp1Data() {
  $.ajax({
    type: "POST",
    url: `${baseUrl}/smartLamp/stm32/getSTM32Info`,
    beforeSend: function (request) {
      request.setRequestHeader("token", localStorage.getItem("token"));
    },
    success: function (res) {
      if (res.stm32LightStatus) {
        $(".btn").css("background-color", "white");
        $(".btn").css("color", "green");
      } else {
        $(".btn").css("background-color", "black");
        $(".btn").css("color", "green");
      }

      // $(".voltage_data").html(res.data.eq1Voltage);
      // $(".electronic_data").html(res.data.eq1Electricity);
      $(".light_data").html(res.stm32LightIntensity);
      // $(".temperature_data").html(res.data.enTemperature);
      // $(".humidity_data").html(res.data.enHumidity);
      // $(".noise_data").html(res.data.enNoise);
      $(".PM2_5_data").html(res.stm32Pm25);
      $(".PM10_data").html(res.stm32Pm10);
      $(".datatime").html(dateTr(res.stm32Time));
      $(".electronic_data").html(res.stm32Battery + "%");
      voltage_data = (res.stm32Battery * 1.2) / 100 + 10.8;
      $(".voltage_data").html(voltage_data.toFixed(2));
    },
  });
  $.ajax({
    type: "POST",
    url: `${baseUrl}/smartLamp/stm32/getSTM32InfoGps`,
    dataType: "json",
    contentType: "application/json",
    beforeSend: function (request) {
      request.setRequestHeader("token", localStorage.getItem("token"));
    },
    data: JSON.stringify({
      lightId: 1,
    }),
    success: function (res) {
      $(".longitude_data").html(res.data.longitude);
      $(".latitude_data").html(res.data.latitude);
      if (!isFirst)
        addMark(
          res.data.longitude.slice(0, -1),
          res.data.latitude.slice(0, -1)
        );
    },
  });
}
function insertLamp2Data() {
  $.ajax({
    type: "POST",
    url: `${baseUrl}/smartLamp/environment/getAllEnInfo`,
    beforeSend: function (request) {
      request.setRequestHeader("token", localStorage.getItem("token"));
    },
    success: function (res) {
      if (res.data.eq1Electricity < 10 || res.data.illuminance < 200) {
        $(".btn1").css("background-color", "white");
        $(".btn1").css("color", "green");
      } else {
        $(".btn1").css("background-color", "black");
        $(".btn1").css("color", "green");
      }

      $(".voltage_data1").html(res.data.eq1Voltage);
      $(".electronic_data1").html(res.data.eq1Electricity + "%");
      $(".light_data1").html(res.data.illuminance);
      $(".temperature_data1").html(res.data.enTemperature / 10);
      $(".humidity_data1").html(res.data.enHumidity);
      $(".noise_data1").html(res.data.enNoise);
      $(".PM2_5_data1").html(res.data.enPm25);
      $(".PM10_data1").html(res.data.enPm10);
      $(".datatime1").html(dateTr(res.data.enTime));
    },
  });
  $.ajax({
    type: "POST",
    url: `${baseUrl}/smartLamp/stm32/getSTM32InfoGps`,
    dataType: "json",
    contentType: "application/json",
    beforeSend: function (request) {
      request.setRequestHeader("token", localStorage.getItem("token"));
    },
    data: JSON.stringify({
      lightId: 1,
    }),
    success: function (res) {
      $(".longitude_data1").html(res.data.longitude);
      $(".latitude_data1").html(res.data.latitude);
      if (!isFirst)
        addMark(
          res.data.longitude.slice(0, -1),
          res.data.latitude.slice(0, -1)
        );
    },
  });
  isFirst = false;
}
function queryWarnInfo(id) {
  $.ajax({
    type: "POST",
    url: `${baseUrl}/smartLamp/stm32/getSTM32alarm`,
    dataType: "json",
    contentType: "application/json",
    beforeSend: function (request) {
      request.setRequestHeader("token", localStorage.getItem("token"));
    },
    data: JSON.stringify({
      lampId: id,
    }),
    success: function (res) {
      if ($(".soundaudio")[0]) {
        if (res.data.alarmState === 1) {
          console.log("报警");
          $("body").click();
          $(".soundaudio")[0].play();
          $(`.audioplay${id}`).addClass("hidden");
          $(`.audiopause${id}`).removeClass("hidden");
        } else {
          $(".soundaudio")[0].pause();
          $(`.audioplay${id}`).removeClass("hidden");
          $(`.audiopause${id}`).addClass("hidden");
        }
      }
    },
  });
}
